import Vue from 'vue'
import user_api from '../api/user'
import client from './client'
// const {
//     app
// } = require('electron').remote


export class UpdateApp {
    constructor() {
        this.versionMessage = {}
    }
    // static  checkUpdate(version){
    //     let oldVersion = Vue.prototype.$readIniFile('data', 'ver')
    //     if (res.code !== '0') {
    //         this.$message.error('检查更新失败，请稍后重试')
    //     }
    //     if (version < oldVersion) {
    //         if (data.isSilent === '1') {
    //             this.$message.success('您当前已是最新版本')
    //             localStorage.setItem('isSilent', '1')
    //         } else {
    //             this.versionMessage = data
    //             return data.currentVersion
    //         }

    //     } else {
    //         this.$message.success('您当前已是最新版本')
    //     }
    //     return ''
    // }
    /**
     * 
     * @param {启动更新exe的路径} exePath 
     * @param {启动exe的参数-type:arry; arry[0]:更新包的下载地址;  arry[1]:'/z'-全量更新,'/e'-增量更新;  arry[2]:'/s'-静默更新 ,''-显示更新} exeArry 
     * @param {是否强制更新-type:Boolean} force 
     */
    static update(exePath, exeArry, options, force = false) {
        let defaultOptions = {
            cancel: "取消",
            confirm: '立即更新',
            message: '发现新版本',
            update: true,
            version:''
        }
        defaultOptions = Object.assign(defaultOptions, options)
        Vue.prototype.$dialog(defaultOptions).then(() => {
            client.executeExe(exePath, exeArry)
            user_api.createUserVersion({
                type:'1',
                currentVersion:localStorage.oldVersion,
                newVersion:options.version
            }).then(res=>{
                console.log(res);
            })
            Vue.prototype.$client.closeAllWindow();
        }).catch(() => {
            if (force) {
                Vue.prototype.$client.closeAllWindow();
            }else{
                // console.log(options);
                // console.log(localStorage);
                user_api.createUserVersion({
                    type:'0',
                    currentVersion:localStorage.oldVersion,
                    newVersion:options.version
                }).then(res=>{
                    console.log(res);
                })
            }
        })
    }
}